package com.ymdd.library.pickerview.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.*;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import com.ymdd.library.R;
import com.ymdd.library.pickerview.listener.OnDismissListener;
import com.ymdd.library.pickerview.utils.PickerViewAnimateUtil;

/**
 * Created by Sai on 15/11/22.
 * 精仿iOSPickerViewController控件
 */
public class BasePickerView {
  private final FrameLayout.LayoutParams params =
      new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
          ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);

  private Context context;
  protected ViewGroup contentContainer;
  private ViewGroup decorView;//activity的根View
  private ViewGroup rootView;//附加View 的 根View
  private ViewGroup dialogView;//附加Dialog 的 根View

  protected int pickerview_timebtn_nor = 0xFF057dff;
  protected int pickerview_timebtn_pre = 0xFFc2daf5;
  //    protected int pickerview_bg_topbar = 0xFFf5f5f5;
  protected int pickerview_bg_topbar = 0xFFFFFFFF;
  protected int pickerview_topbar_title = 0xFF000000;
  protected int bgColor_default = 0xFFFFFFFF;

  private OnDismissListener onDismissListener;
  private boolean dismissing;

  private Animation outAnim;
  private Animation inAnim;
  private boolean isShowing;
  private int gravity = Gravity.BOTTOM;

  private Dialog mDialog;
  private boolean cancelable;//是否能取消

  protected View clickView;//是通过哪个View弹出的

  public BasePickerView(Context context) {
    this.context = context;

        /*initViews();
        init();
        initEvents();*/
  }

  protected void initViews() {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    if (isDialog()) {
      //如果是对话框模式
      dialogView = (ViewGroup) layoutInflater.inflate(R.layout.layout_basepickerview, null, false);
      //设置界面的背景为透明
      dialogView.setBackgroundColor(Color.TRANSPARENT);
      //这个是真正要加载时间选取器的父布局
      contentContainer = (ViewGroup) dialogView.findViewById(R.id.content_container);
      //设置对话框 左右间距屏幕30
      this.params.leftMargin = 30;
      this.params.rightMargin = 30;
      contentContainer.setLayoutParams(this.params);
      //创建对话框
      createDialog();
      //给背景设置点击事件,这样当点击内容以外的地方会关闭界面
      dialogView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
          dismiss();
        }
      });
    } else {
      //如果只是要显示在屏幕的下方
      //decorView是activity的根View
      decorView = (ViewGroup) ((Activity) context).getWindow()
          .getDecorView()
          .findViewById(android.R.id.content);
      //将控件添加到decorView中
      rootView =
          (ViewGroup) layoutInflater.inflate(R.layout.layout_basepickerview, decorView, false);
      rootView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
          ViewGroup.LayoutParams.MATCH_PARENT));
      //这个是真正要加载时间选取器的父布局
      contentContainer = (ViewGroup) rootView.findViewById(R.id.content_container);
      contentContainer.setLayoutParams(params);
    }

    setKeyBackCancelable(true);
  }

  protected void init() {
    inAnim = getInAnimation();
    outAnim = getOutAnimation();
  }

  protected void initEvents() {
  }

  /**
   * show的时候调用
   *
   * @param view 这个View
   */
  private void onAttached(View view) {
    decorView.addView(view);
    contentContainer.startAnimation(inAnim);
  }

  /**
   * 添加这个View到Activity的根视图
   */
  public void show() {
    if (isDialog()) {
      showDialog();
    } else {
      if (isShowing()) {
        return;
      }
      isShowing = true;
      onAttached(rootView);
      rootView.requestFocus();
    }
  }

  /**
   * 添加这个View到Activity的根视图
   *
   * @param v (是通过哪个View弹出的)
   */
  public void show(View v) {
    this.clickView = v;
    show();
  }

  /**
   * 检测该View是不是已经添加到根视图
   *
   * @return 如果视图已经存在该View返回true
   */
  public boolean isShowing() {
    if (isDialog()) {
      return false;
    } else {
      return rootView.getParent() != null || isShowing;
    }
  }

  public void dismiss() {
    if (isDialog()) {
      dismissDialog();
    } else {
      if (dismissing) {
        return;
      }

      dismissing = true;

      //消失动画
      outAnim.setAnimationListener(new Animation.AnimationListener() {
        @Override public void onAnimationStart(Animation animation) {

        }

        @Override public void onAnimationEnd(Animation animation) {
          decorView.post(new Runnable() {
            @Override public void run() {
              dismissImmediately();
            }
          });
        }

        @Override public void onAnimationRepeat(Animation animation) {

        }
      });
      contentContainer.startAnimation(outAnim);
    }
  }

  public void dismissImmediately() {
    //从activity根视图移除
    decorView.removeView(rootView);
    isShowing = false;
    dismissing = false;
    if (onDismissListener != null) {
      onDismissListener.onDismiss(BasePickerView.this);
    }
  }

  public Animation getInAnimation() {
    int res = PickerViewAnimateUtil.getAnimationResource(this.gravity, true);
    return AnimationUtils.loadAnimation(context, res);
  }

  public Animation getOutAnimation() {
    int res = PickerViewAnimateUtil.getAnimationResource(this.gravity, false);
    return AnimationUtils.loadAnimation(context, res);
  }

  public BasePickerView setOnDismissListener(OnDismissListener onDismissListener) {
    this.onDismissListener = onDismissListener;
    return this;
  }

  public BasePickerView setKeyBackCancelable(boolean isCancelable) {

    ViewGroup View;
    if (isDialog()) {
      View = dialogView;
    } else {
      View = rootView;
    }

    View.setFocusable(isCancelable);
    View.setFocusableInTouchMode(isCancelable);
    if (isCancelable) {
      View.setOnKeyListener(onKeyBackListener);
    } else {
      View.setOnKeyListener(null);
    }
    return this;
  }

  private View.OnKeyListener onKeyBackListener = new View.OnKeyListener() {
    @Override public boolean onKey(View v, int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK
          && event.getAction() == MotionEvent.ACTION_DOWN
          && isShowing()) {
        dismiss();
        return true;
      }
      return false;
    }
  };

  protected BasePickerView setOutSideCancelable(boolean isCancelable) {
    if (rootView != null) {
      View view = rootView.findViewById(R.id.outmost_container);

      if (isCancelable) {
        view.setOnTouchListener(onCancelableTouchListener);
      } else {
        view.setOnTouchListener(null);
      }
    }

    return this;
  }

  /**
   * 设置对话框模式是否可以点击外部取消
   */
  public void setDialogOutSideCancelable(boolean cancelable) {
    this.cancelable = cancelable;
  }

  /**
   * Called when the user touch on black overlay in order to dismiss the dialog
   */
  private final View.OnTouchListener onCancelableTouchListener = new View.OnTouchListener() {
    @Override public boolean onTouch(View v, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        dismiss();
      }
      return false;
    }
  };

  public View findViewById(int id) {
    return contentContainer.findViewById(id);
  }

  public void createDialog() {
    if (dialogView != null) {
      mDialog = new Dialog(context, R.style.custom_dialog2);
      mDialog.setCancelable(cancelable);//不能点外面取消,也不 能点back取消
      mDialog.setContentView(dialogView);
      mDialog.getWindow().setWindowAnimations(R.style.pickerview_dialogAnim);
      mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override public void onDismiss(DialogInterface dialog) {
          if (onDismissListener != null) {
            onDismissListener.onDismiss(BasePickerView.this);
          }
        }
      });
    }
  }

  public void showDialog() {
    if (mDialog != null) {
      mDialog.show();
    }
  }

  public void dismissDialog() {
    if (mDialog != null) {
      mDialog.dismiss();
    }
  }

  public boolean isDialog() {
    return false;
  }
}
